﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using TongHopBookFlightDAO;
using TongHopBookFlightDAO.DAO;

namespace BookFlightUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Thread ThreadMain001 = null;
        List<SearchInfo> lstSearchInfos= new List<SearchInfo>();

        private void btnRutTrichChuyenBay_Click(object sender, EventArgs e)
        {
            List<DiaDiem> lstDiaDiems = new List<DiaDiem>();

            DateTime ngayBatDau = dtNgayBatDau.Value;
            DateTime ngayKetThuc = dtNgayKetThuc.Value;

            int soNgay = ngayBatDau.Day - ngayKetThuc.Day + 1;

            HangHangKhong hangkhong = HangHangKhongDAO.SelectByName("AirMekong");
            List<ChiTietDiaDiem> lstChiTietDiaDiems = new List<ChiTietDiaDiem>();
            if (hangkhong != null)
            {
                ////Get danh sach dia diem chi tiet
                lstChiTietDiaDiems = ChiTietDiaDiemDAO.SelectByAirLineID(hangkhong.HangHangKhongID);

                for (int i = 0; i < lstChiTietDiaDiems.Count; i++)
                {
                    DiaDiem diadiem = DiaDiemDAO.SelectByID(lstChiTietDiaDiems[i].DiaDiemID.Value);
                   if (diadiem != null)
                       lstDiaDiems.Add(diadiem);
                }
          

                if (soNgay >= 0)
                {
                    for (int i = 0; i < soNgay; i++)
                    {
                        for (int j = 0; j < lstDiaDiems.Count; j++)
                            for (int k = 0; k < lstDiaDiems.Count; k++)
                                if (lstDiaDiems[j].TenDiaDiem.CompareTo(lstDiaDiems[k].TenDiaDiem) != 0)
                                {
                                    SearchInfo searchInfo = new SearchInfo();

                                    searchInfo.diemDi = lstDiaDiems[j].MaDiaDiem;
                                    searchInfo.diemDen = lstDiaDiems[k].MaDiaDiem;
                                    searchInfo.ngayDi = ngayBatDau;

                                    lstSearchInfos.Add(searchInfo);
                                }

                        ngayBatDau = ngayBatDau.AddDays(1);
                    }

                    ThreadMain001 = new Thread(ThreadAirMekong);
                    ThreadMain001.IsBackground = true;
                    ThreadMain001.Start();
                }
            }


        }

        void ThreadAirMekong()
        {
            TongHopBookFlightCores.Cores.RutTrichTrangAirMekong rutTrichTrangAirMekong = new TongHopBookFlightCores.Cores.RutTrichTrangAirMekong();

            for (int i = 0; i < lstSearchInfos.Count; i++)
            {
                SearchInfo searchInfo = lstSearchInfos[i];

                rutTrichTrangAirMekong.TimChuyenBayTheoMotChieu(searchInfo.diemDi, searchInfo.diemDen, searchInfo.ngayDi);
            }
           
            MessageBox.Show("Rút trích hoàn thành");

        }

        void ThreadVietNamAirlines()
        {
            TongHopBookFlightCores.Cores.RutTrichTrangVietNamAirlines rutTrichTrangVietNamAirlines = new TongHopBookFlightCores.Cores.RutTrichTrangVietNamAirlines();

             for (int i = 0; i < lstSearchInfos.Count; i++)
            {
                 SearchInfo searchInfo = lstSearchInfos[i];

                 rutTrichTrangVietNamAirlines.TimChuyenBayTheoMotChieu(searchInfo.diemDi, searchInfo.diemDen, searchInfo.ngayDi);
            }

             MessageBox.Show("Rút trích hoàn thành");
        }

        void ThreadJetstar()
        {
            TongHopBookFlightCores.Cores.RutTrichTrangJetstar rutTrichTrangJetstar = new TongHopBookFlightCores.Cores.RutTrichTrangJetstar();

             for (int i = 0; i < lstSearchInfos.Count; i++)
            {
                 SearchInfo searchInfo = lstSearchInfos[i];

                 rutTrichTrangJetstar.TimChuyenBayTheoMotChieu(searchInfo.diemDi, searchInfo.diemDen, searchInfo.ngayDi);
            }

             MessageBox.Show("Rút trích hoàn thành");
        }

        
        private void Form1_Load(object sender, EventArgs e)
        {
            dtNgayBatDau.Value = DateTime.Today.AddDays(1);
            dtNgayKetThuc.Value = DateTime.Today.AddDays(1);

        }
    }
}
